/**
 * 请在白鹭引擎的Main.ts中调用 platform.login() 方法调用至此处。
 */

class WxgamePlatform {

  name = 'wxgame'
  /**登录授权 */
  login() {
    return new Promise((resolve, reject) => {
      wx.login({
        success: (res) => {
          resolve(res);
        }
      })
    })
  }
  /**获取自己信息 */
  getUserInfo(xx) {
    return new Promise((resolve, reject) => {
      wx.getSystemInfo({
        success: function(res) {
          let button = wx.createUserInfoButton({
            type: 'image',
            image: 'openDataContext/assets/start.png',
            style: {
              left: res.screenWidth/2-71.5,
              top: res.screenHeight/2,
              width: 143,
              height: 159,
              lineHeight: 40,
              backgroundColor: '#ff0000',
              color: '#ffffff',
              textAlign: 'center',
              fontSize: 16,
              borderRadius: 4
            }
          })
          button.onTap((res) => {
            console.log(res.rawData);
            resolve(res.userInfo);
            button.destroy();
          })
          
        },
      })
    })
  }
  //创建获取玩家信息按钮
  createUserInfoButton(xx) {
    let {
      screenWidth,
      screenHeight
    } = wx.getSystemInfoSync();
    let but = wx.createUserInfoButton({
      type: 'text',
      text: "", //'点击打开设置页面',
      style: {
        left: 0, // screenWidth * 0.25,
        top: 0, //screenHeight * 0.7,
        width: screenWidth, //200,
        height: screenHeight, //40,
        lineHeight: 40,
      }
    })
    but.onTap((res) => {
      if (res.errMsg.indexOf('auth deny') > -1 || res.errMsg.indexOf('auth denied') > -1) {
        console.log("button.onTap:" + res)
        console.log(but);
      } else {
        but.hide();
        but.destroy();
        xx();
      }
    })
  }
  getSetting() {
    return new Promise((resolve, reject) => {
      wx.getSetting({
        success(res) {
          if (res.authSetting['scope.userInfo']) {
            console.log("wx.getSetting:用户授权");
          } else {
            console.log("wx.getSetting:用户拒绝授权");
          }
        }
      })
    })
  }
  /**微信分享 */
  showShareMenu() {
    return new Promise((resolve, reject) => {
      wx.showShareMenu({
        withShareTicket: true,
        success: res => {
          console.log("分享接口调用成功");
          this.onshareMessage();
          resolve(res);
        },
        fail: res => {
          console.log("分享接口调用失败");
        },
        complete: res => {
          console.log("分享接口调用失败");
        }
      })
    })
  }
  /**微信分享内容 */
  onshareMessage() {
    wx.onShareAppMessage(() => {
      return {
        title: "舌战群蛙",
        imageUrl: "openDataContext/assets/rankingtitle.png",
        query: "key1=" + openid,
        success: res => {
          console.log("转发成功" + JSON.stringify(res));
        },
        fail: function(res) {
          console.log("转发失败" + res.errMsg);
        },
        complete: function() {
          console.log("转发接口调用完成");
        }
      }
    })
  }
  /**自制微信分享+内容 */
  activeShare() {
    return new Promise((resolve, reject) => {
      var data = null;
      wx.shareAppMessage({
        title: "舌战群蛙",
        imageUrl: "openDataContext/assets/box.png",
        query: "key1=" + openid,
        success: function(res) {
          console.log("转发成功" + JSON.stringify(res));
          data = res;
        },
        fail: function(res) {
          console.log("转发失败" + res.errMsg);
          data = res;
        },
        complete: function() {
          console.log("转发接口调用完成");
          resolve(data);
        }
      })
    })
  }
  findShareQuery() {
    wx.onShow(
      (res) => {
        if (res.scene === 1044) {
          console.log(res.query.key1);
        }
        console.log("*****show************");
      })
  }
  /**设置用户的云存储 */
  setUserCloudStorage(score) {
    return new Promise((resolve, reject) => {
      wx.setUserCloudStorage({
        KVDataList: [{
          key: 'score',
          value: score
        }],
        success: res => {
          console.log(res);
        },
        fail: res => {
          console.log(res);
        }
      });
    })
  }
  openDataContext = new WxgameOpenDataContext();
}
class WxgameOpenDataContext {

  createDisplayObject(type, width, height) {
    const bitmapdata = new egret.BitmapData(sharedCanvas);
    bitmapdata.$deleteSource = false;
    const texture = new egret.Texture();
    texture._setBitmapData(bitmapdata);
    const bitmap = new egret.Bitmap(texture);
    bitmap.width = width;
    bitmap.height = height;

    const renderContext = egret.wxgame.WebGLRenderContext.getInstance();
    const context = renderContext.context;
    ////需要用到最新的微信版本
    ////调用其接口WebGLRenderingContext.wxBindCanvasTexture(number texture, Canvas canvas)
    ////如果没有该接口，会进行如下处理，保证画面渲染正确，但会占用内存。
    if (!context.wxBindCanvasTexture) {
      egret.startTick((timeStarmp) => {
        egret.WebGLUtils.deleteWebGLTexture(bitmapdata.webGLTexture);
        bitmapdata.webGLTexture = null;
        return false;
      }, this);
    }
    return bitmap;
  }


  postMessage(data) {
    const openDataContext = wx.getOpenDataContext();
    openDataContext.postMessage(data);
  }
}
window.platform = new WxgamePlatform();
var openid = null;
var appid = "wx666129ed333866c7";
var appsecret = "6039c2f75a275f7ab7d3f1f59007b921";